package com.bestcem.xm.om.grpc.service.converter;

import com.bestcem.bp.pay.grpc.v1.services.Order;
import com.bestcem.xm.common.core.uitls.GrpcDateUtil;
import com.bestcem.xm.om.grpc.service.dto.OrderDTO;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * 订单grpc请求出入参转化类
 *
 * @author YiFang <yifang.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/6 20:34
 */
@Data
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class OmOrderConverter {
    /**
     * grpc请求入参转换成本地入参：套餐升级
     *
     * @return
     */
    public static OrderDTO convertToOrderDTO(Order order) {
        if (order == null) {
            return null;
        }

        OrderDTO orderDTO = new OrderDTO();

        orderDTO.setPayMethod(order.getPayMethodValue());
        orderDTO.setType(order.getTypeValue());
        orderDTO.setStatus(order.getStatusValue());
        orderDTO.setPayConfirmed(Boolean.TRUE.equals(order.getPayConfirmed()) ? NumberUtils.INTEGER_ONE : NumberUtils.INTEGER_ZERO);

        if (order.hasPayTime()) {
            orderDTO.setPayTime(GrpcDateUtil.getDate(order.getPayTime()));
        }
        if (order.hasBaseTime() && order.getBaseTime() != null && order.getBaseTime().hasCreateTime()) {
            orderDTO.setCreateTime(GrpcDateUtil.getDate(order.getBaseTime().getCreateTime()));
        }
        if (order.hasBaseTime() && order.getBaseTime() != null && order.getBaseTime().hasUpdateTime()) {
            orderDTO.setUpdateTime(GrpcDateUtil.getDate(order.getBaseTime().getUpdateTime()));
        }
        if (order.getWalletId() != null) {
            orderDTO.setWalletId(order.getWalletId());
        }
        if (order.getOrderNo() != null) {
            orderDTO.setOrderNo(order.getOrderNo());
        }
        if (order.getSubject() != null) {
            orderDTO.setSubject(order.getSubject());
        }
        if (order.getDescription() != null) {
            orderDTO.setDescription(order.getDescription());
        }
        if (order.getPayerUserId() != null) {
            orderDTO.setPayerUserId(order.getPayerUserId());
        }
        if (order.getPayerUserName() != null) {
            orderDTO.setPayerUserName(order.getPayerUserName());
        }
        if (order.getPayerOrgId() != null) {
            orderDTO.setPayerOrgId(order.getPayerOrgId());
        }
        if (order.getPayerOrgCode() != null) {
            orderDTO.setPayerOrgCode(order.getPayerOrgCode());
        }
        if (order.getPayerOrgName() != null) {
            orderDTO.setPayerOrgName(order.getPayerOrgName());
        }
        if (order.getPayerAccountNo() != null) {
            orderDTO.setPayerAccountNo(order.getPayerAccountNo());
        }
        orderDTO.setTotalAmount(order.getTotalAmount());
        orderDTO.setReceiptAmount(order.getReceiptAmount());
        if (order.getOutOrderNo() != null) {
            orderDTO.setOutOrderNo(order.getOutOrderNo());
        }
        if (order.getExtra() != null) {
            orderDTO.setExtra(order.getExtra());
        }

        return orderDTO;
    }
}
